#include <QPainter>
#include <QPalette>
#include "circlelabel.h"
#include "common.h"

CircleLabel::CircleLabel(int width, int height, QWidget* parent) : QLabel(parent), MARGIN(6)
{
    if(width && height)
    {
        setFixedSize(width, height);
    }
}

void CircleLabel::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    // 设置反走样: 反锯齿, 平滑效果
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    if(m_flag)
    {
        QPen pen(QColor(255, 255, 255, 150));
        pen.setWidth(MARGIN/2);
        painter.setPen(pen);
        painter.drawEllipse(1, 1, width()-2, height()-2);
    }
    QPainterPath path;
    // 求最小值作为圆形的直径
    int round = qMin(width()-MARGIN, height()-MARGIN);
    path.addEllipse(MARGIN/2, MARGIN/2, round, round);
    painter.setClipPath(path);
    painter.drawPixmap(MARGIN/2, MARGIN/2, round, round, QPixmap(m_image));

}

// 鼠标进入
void CircleLabel::enterEvent(QEvent *)
{
    m_flag = true;
    update();
}

// 鼠标离开
void CircleLabel::leaveEvent(QEvent *)
{
    m_flag = false;
    update();
}
